//tslint:disable
import { Component, OnInit, Input, OnDestroy } from "@angular/core";
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { Subscription } from "rxjs";
// import { MessageService } from "src/app/shared/service/message.service";
import { EventService } from 'src/app/shared/service/event.service';
import { HttpService } from "../../shared/service/http.service";
import { AppGlobalService } from "src/app/shared/service/app-global.service";
declare var $;
@Component({
  selector: 'app-my-diy-list',
  templateUrl: './my-diy-list.component.html',
  styleUrls: ['./my-diy-list.component.scss'],
})
export class MyDiyListComponent implements OnInit {
  params: any;
  @Input() tagData: any;
  pageNo: any = 1;
  sessionId = localStorage.getItem("sessionId");
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private eventService: EventService,
    // private message: MessageService,
    private httpService: HttpService,
  ) { }

  ngOnInit() {
    const listenerName = this.tagData.name + 'AskForList';
    this.eventService.eventEmit.on(listenerName, (obj) => {
      this.refreshDataByEvent(obj);
    });
    this.params = this.tagData.data.submit;
    // this.message.clearMessage();
  }
  goPage(event, item) {
    event.stopPropagation();
    this.pageModalService.openPage(item.link);
  }

  refreshDataByEvent(param) {
    const tempParam = {
      sessionId: localStorage.getItem("sessionId"),
      params: { search: param.params, eventId: this.tagData.data.footDiv.button.text.eventId },
      pageId: param.target
    };
    // 请求获取新数据
    this.httpService.post("application/page", tempParam, res => {
      // const pageData = res.s
      // 请求数据成功
      if (res.script) {
        let dataArray = [];
        const tags = JSON.parse(res.script).views[0].tags;
        tags.forEach(element => {
          if (element.style === 'counselorChooseListTag') {
            dataArray = element.data.data;
          }
        });

        if (Array.isArray(dataArray) && dataArray.length > 0) {
          this.tagData.data.data = dataArray;

          // this.params = res.data.submit;
        } else {
          this.tagData.data.data = [];
          // 没有数据了
          // this.appGlobalService.showToast("没有数据了...", 1000, "bottom");
        }

      } else {
        // 请求失败，更新当前页数
        this.tagData.data.data = [];
        this.pageNo = 1;
        // 请求数据失败
        this.appGlobalService.showToast(res.message, 2000, "middle");
      }
    });
  }

  loadData(event) {
    setTimeout(() => {
      this.params.sessionId = localStorage.getItem("sessionId");
      // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      this.params.content.pageNo = this.pageNo;
      // 请求获取新数据
      this.httpService.post("application/submit", this.params, res => {
        // 请求数据成功
        if (res.success) {
          // 追加返回数据
          if (Array.isArray(res.data.data) && res.data.data.length > 0) {
            // 追加数据
            res.data.data.map(item => {
              this.tagData.data.data.push(item);

            });
            // this.params = res.data.submit;
          } else {
            // 没有数据了
            // this.appGlobalService.showToast("没有数据了...", 1000, "bottom");
          }
          event.target.complete();
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, "middle");
          event.target.complete();
        }
      });
    }, 500);
  }

}
